var app = getApp();
var api = require('../../config/api.js');
var utils = require('../../utils/util.js');
var QRCode = require('../../utils/weapp-qrcode.js')
import rpx2px from '../../utils/rpx2px.js'
var qrcode;

const W = business.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
const qrcode_w = 300 / rate;

Page({
  data: {
    sex: 1,
    complete: 0,
    dept_id: '',
    dept_name: '',
    qrcode_w: qrcode_w,
    radios: [
      {
        label: '男',
        value: 1,
      },
      {
        label: '女',
        value: 0,
      }
    ],
  },
  
  onLoad: function (options) {
    var obj = business.getLaunchOptionsSync();
    console.log('启动系统的 query 参数:', obj.query);
    this.setData({
      dept_id: obj.query.dept_id,
      dept_name: obj.query.dept_name
    })
  },
  
  check: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log("check选择的值是:" + index);
    that.setData({
      sex: that.data.radios[index].value
    })
  },
  
  userForm: function(e) {
    var that = this;
    var name = e.detail.value.name;
    var icard = e.detail.value.icard;
    var mobile = e.detail.value.mobile;
    var desc = e.detail.value.desc;
    var sex = that.data.sex;
    
    if (name == "" || name == null) {
      business.showModal({
        title: '提示',
        content: '姓名不能为空',
        showCancel: false
      })
      return;
    }
    
    if (icard == "" || icard == null) {
      business.showModal({
        title: '提示',
        content: '身份证号不能为空',
        showCancel: false
      })
      return;
    }
    
    if (mobile == "" || mobile == null) {
      business.showModal({
        title: '提示',
        content: '手机号不能为空',
        showCancel: false
      })
      return;
    }
    
    var re = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (!re.test(mobile)) {
      business.showModal({
        title: '提示',
        content: '手机号格式不正确',
        showCancel: false
      })
      return;
    }
    
    if (mobile.length > 11) {
      business.showModal({
        title: '提示',
        content: '手机号格式不正确',
        showCancel: false
      })
      return;
    }
    
    var params = new Object();
    params.guestName = name;
    params.mobile = mobile;
    params.ssex = sex;
    params.description = desc;
    params.icard = icard;
    params.deptId = that.data.dept_id;
    params.location = that.data.dept_name;
    
    console.log(params);
    utils.request(api.Guest, params).then(function (res) {
      console.log(res);
      if (res.code == 1) {
        var qrcode = new QRCode('canvas', {
          width: qrcode_w,
          height: qrcode_w,
          colorDark: "#1CA4FC",
          colorLight: "white",
          correctLevel: QRCode.CorrectLevel.H,
        });
        qrcode.makeCode(mobile);
        that.setData({
          complete: 1,
        });
      } else {
        business.showModal({
          title: '提示',
          content: res.message,
          showCancel: false
        })
      }
    })
  },
})